---
title: "Subgroup analysis: gender"
author: "[redacted]"
date: "2025-04-07"
output: html_document
---

```{r}
```

#####LOAD PACKAGES AND DATA

```{r}

#Load packages
library(tidyverse)
library(cjoint)
library(cregg)

getwd()

#Load data
conjoint_gend <- cjoint::read.qualtrics(
  "data_clean.csv",
  covariates = c("gender"),
  respondentID = "ResponseId",
  letter = "F",
  new.format = TRUE,
  responses = paste0("conjoint_", c("intro", 1:8))
)

#Remove NAs
conjoint_gend <- conjoint_gend %>% drop_na(selected)

#Check names
names(conjoint_gend)

```

#RECODE COVARIATE

```{r}

conjoint_gend <- conjoint_gend %>%
  mutate(
    gender_cat = ifelse(gender == 0, "Male",
              ifelse(gender == 1, "Female", NA)),
    gender_cat = factor(gender_cat, levels=c("Male", "Female"))  # Convert gender to a factor
  )

conjoint_gend <- conjoint_gend %>% drop_na(gender_cat)

summary(conjoint_gend$gender_cat)

```

#RENAME VARIABLES

```{r}

# Renaming attributes and levels in English
conjoint_gend <-  conjoint_gend %>% dplyr::rename(Topic=Onderwerp, 
                                            Relation.to.you=Relatie.tot.u,
                                            Apparent.knowledge.of.the.person=Schijnbare.kennis.van.de.persoon,
                                            Apparent.concern.of.the.person=Schijnbare.bezorgdheid.van.de.persoon,
                                            Apparent.motivation.of.the.person=Schijnbare.motivatie.van.de.persoon,
                                            Closeness.with.you=Band.met.u,
                                            Gender.of.the.person=Geslacht.van.de.persoon
                                            ) %>%
                                     mutate(
                                            Topic = recode_factor(Topic,
                                                  "overstromingen" = "floods", 
                                                  "besmettelijke ziekten" = "infectious diseases",
                                                  "pesticiden" = "pesticides",
                                                  "levensstijlziekten" = "lifestyle diseases",),
                                            Gender.of.the.person = recode_factor(Gender.of.the.person,
                                                  "vrouw" = "female", 
                                                  "man" = "male"),
                                            Relation.to.you = recode_factor(Relation.to.you,
                                                  "vriend/vriendin (platonisch)" = "friend",
                                                  "collega/studiegenoot" = "colleague/fellow student",
                                                  "familielid" = "relative"),
                                            Closeness.with.you = recode_factor(Closeness.with.you,
                                                  "zeer hecht" = "very close", 
                                                  "redelijk hecht" = "fairly close",
                                                  "enigszins hecht" = "somewhat close",
                                                  "niet hecht" = "not close"),
                                            Apparent.knowledge.of.the.person = recode_factor(Apparent.knowledge.of.the.person,
                                                  "zeer goed geinformeerd" = "very well-informed", 
                                                  "redelijk goed geinformeerd" = "fairly well-informed",
                                                  "enigszins geinformeerd" = "somewhat well-informed",
                                                  "niet geinformeerd" = "not well-informed"),
                                            Apparent.concern.of.the.person = recode_factor(Apparent.concern.of.the.person,
                                                  "zeer bezorgd" = "very concerned", 
                                                  "redelijk bezorgd" = "fairly concerned",
                                                  "enigszins bezorgd" = "somewhat concerned",
                                                  "niet bezorgd" = "not concerned"),
                                            Apparent.motivation.of.the.person = recode_factor(Apparent.motivation.of.the.person,
                                                  "u te overtuigen om meer maatregelen te nemen" = "to convince you to take more precautions", 
                                                  "informatie over het risico met u te willen uitwisselen" = "to exchange information with you",
                                                  "zich minder ongerust voelen" = "to feel less anxious",
                                                  "u te overtuigen om minder maatregelen te nemen" = "to convince you to take fewer precautions"))


#Check renamings
table(conjoint_gend$Topic)
table(conjoint_gend$Gender.of.the.person)
table(conjoint_gend$Relation.to.you)
table(conjoint_gend$Closeness.with.you)
table(conjoint_gend$Apparent.knowledge.of.the.person)
table(conjoint_gend$Apparent.concern.of.the.person)
table(conjoint_gend$Apparent.motivation.of.the.person)


```

#CHANGE VARIABLE ORDER AND SET REFERENCE CATEGORIES

```{r}

#REORDER LEVELS
conjoint_gend$Topic <- factor(conjoint_gend$Topic,
                                           levels = c("pesticides", "floods", "lifestyle diseases", "infectious diseases"))
conjoint_gend$Gender.of.the.person <- factor(conjoint_gend$Gender.of.the.person,
                                           levels = c(  "male", "female"))
conjoint_gend$Relation.to.you <- factor(conjoint_gend$Relation.to.you,
                                           levels = c("colleague/fellow student", "friend", "relative"))
conjoint_gend$Closeness.with.you <- factor(conjoint_gend$Closeness.with.you,
                                           levels = c("not close", "somewhat close", "fairly close", "very close"))
conjoint_gend$Apparent.knowledge.of.the.person <- factor(conjoint_gend$Apparent.knowledge.of.the.person,
                                           levels = c("not well-informed", "somewhat well-informed", "fairly well-informed", "very well-informed"))
conjoint_gend$Apparent.concern.of.the.person <- factor(conjoint_gend$Apparent.concern.of.the.person,
                                           levels = c("not concerned", "somewhat concerned", "fairly concerned", "very concerned"))
conjoint_gend$Apparent.motivation.of.the.person <- factor(conjoint_gend$Apparent.motivation.of.the.person,
                                           levels = c("to convince you to take more precautions", "to convince you to take fewer precautions", "to feel less anxious", "to exchange information with you"))


```

######RUN MAIN AMCE, MM, AND DIFFERENCES IN MM 

```{r}

###AMCE

amce_gend <- cj(conjoint_gend, selected ~ 
             Topic +
             Gender.of.the.person +
             Relation.to.you +
             Closeness.with.you + 
             Apparent.knowledge.of.the.person +
             Apparent.concern.of.the.person +
             Apparent.motivation.of.the.person, 
                  by = ~ gender_cat, 
                  id = ~ Response.ID,
                  estimate = "amce")

# Define the dodge width to create separation between male and female lines
dodge_width <- 1

amce_gend_plot <- ggplot(amce_gend, aes(x = estimate, y = level, color = gender_cat, shape = gender_cat)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("darkblue", "deeppink4")) +  # Custom colors for gender_cat
  scale_shape_manual(values = c(16, 17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "AMCE", y = "All attributes", color = "Gender", shape = "Gender") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(-0.3, 0.3)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/gender/amce_gend.png", plot = amce_gend_plot, width = 8, height = 6, dpi = 300)


###MARGINAL MEANS

mm_gend <- cj(conjoint_gend, selected ~ 
                  Topic +
                  Gender.of.the.person +
                  Relation.to.you +
                  Closeness.with.you + 
                  Apparent.knowledge.of.the.person +
                  Apparent.concern.of.the.person +
                  Apparent.motivation.of.the.person, 
                  by = ~ gender_cat, 
                  id = ~ Response.ID,
                  estimate = "mm")


mm_gend$feature <- factor(mm_gend$feature,
                              levels = c("Topic",
                                         "Relation.to.you",
                                         "Apparent.knowledge.of.the.person",
                                         "Apparent.concern.of.the.person",
                                         "Apparent.motivation.of.the.person",
                                         "Closeness.with.you",
                                         "Gender.of.the.person"))

# Define the dodge width to create separation between male and female lines
dodge_width <- 1

mm_gend_plot <- ggplot(mm_gend, aes(x = estimate, y = level, color = gender_cat, shape = gender_cat)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0.5, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("darkblue", "deeppink4")) +  # Custom colors for gender_cat
  scale_shape_manual(values = c(16, 17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "Marginal Means", y = "All attributes", color = "Gender", shape = "Gender") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(0.3, 0.7)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/gender/mm_gend.png", plot = mm_gend_plot, width = 8, height = 6, dpi = 300)


###DIFFERENCES IN MARGINAL MEANS

diff_gend <- cj(conjoint_gend, selected ~ 
                  Topic +
                  Gender.of.the.person +
                  Relation.to.you +
                  Closeness.with.you + 
                  Apparent.knowledge.of.the.person +
                  Apparent.concern.of.the.person +
                  Apparent.motivation.of.the.person, 
                  by = ~ gender_cat, 
                  id = ~ Response.ID,
                  estimate = "mm_differences")


diff_gend$feature <- factor(diff_gend$feature,
                              levels = c("Topic",
                                         "Relation.to.you",
                                         "Apparent.knowledge.of.the.person",
                                         "Apparent.concern.of.the.person",
                                         "Apparent.motivation.of.the.person",
                                         "Closeness.with.you",
                                         "Gender.of.the.person"))

# Define the dodge width to create separation between male and female lines
dodge_width <- 1

diff_gend_plot <- ggplot(diff_gend, aes(x = estimate, y = level, color = gender_cat, shape = gender_cat)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("deeppink4")) +  # Custom colors for gender_cat
  scale_shape_manual(values = c(17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "Differences in Marginal Means", y = "All attributes", color = "Gender", shape = "Gender") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  )  +
  xlim(-0.15, 0.15)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/gender/diff_gend.png", plot = diff_gend_plot, width = 8, height = 6, dpi = 300)

```
#BALANCE TESTING

```{r}

# BALANCE TESTING

# Manually map gender_cat to numeric (assuming "Male" and "Female")
conjoint_gend$gender_cat_num <- ifelse(conjoint_gend$gender_cat == "Male", 0, 
                                      ifelse(conjoint_gend$gender_cat == "Female", 1, NA))

# Check for any NA values introduced by this process
table(conjoint_gend$gender_cat_num)

# Calculate marginal means using your dataset and attributes
genbalance <- plot(mm(conjoint_gend, gender_cat_num ~ 
                 Topic +
                 Gender.of.the.person +
                 Apparent.motivation.of.the.person +
                 Apparent.concern.of.the.person +
                 Relation.to.you +
                 Closeness.with.you + 
                 Apparent.knowledge.of.the.person,
                 id = ~Response.ID), 
                xlim = c(0, 1), vline = mean(conjoint_gend$gender_cat_num, na.rm = TRUE))

# Adjust the plot for larger text and dots
genbalance_adj <- genbalance +
  geom_point(size = 3, position = position_dodge(width = 0.5)) +
    ggtitle("Balance Testing: Gender") +  # Add the title here
    labs(color = "Attribute") +  # Change the label for the color legend
  theme(
    text = element_text(size = 20),            # Increase base text size
    axis.title = element_text(size = 22),      # Increase axis title size
    axis.text = element_text(size = 18),       # Increase axis tick labels size
    legend.title = element_text(size = 22),    # Increase legend title size
    legend.text = element_text(size = 18),     # Increase legend text size
    plot.title = element_text(size = 24, face = "bold"),  # Increase plot title size and style
    strip.text = element_text(size = 18)       # Increase facet labels text size (if using facets)
  ) +
  guides(color = guide_legend(nrow = 4))  # Adjust the number of rows in the legend

# Save the plot
ggsave("diagnostics/balance_gender.png", plot = genbalance_adj, width = 15, height = 11, dpi = 300)

```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
